package com.neuedu.shop.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.neuedu.shop.pojo.CartItem;
import com.neuedu.shop.service.CartItemService;

/**
 * @author ZhangHongRui
 *
 *         2020年1月8日
 */
@Controller
public class CartItemController {

	@Autowired
	private CartItemService service;

	@RequestMapping("/pre/insert.cartitem")
	public String insert(CartItem ci) {
		// System.out.println(ci.getUserid());
		if (ci.getUserid() != null) {
			service.insert(ci);
			return "redirect:index.product";
		} else
			return "redirect:user_login.jsp";

	}

	@RequestMapping("/pre/findByUserid.cartitem")
	public String findByUserid(Integer userid, ModelMap map, HttpSession session) {
		List<CartItem> items = service.findByUserid(userid);
		double pad = 0;
		for (int i = 0; i < items.size(); i++) {
			pad = pad + items.get(i).getPcount() * items.get(i).getMemberprice();
		}
		// System.out.println(pad);
		session.setAttribute("size", items.size());
		session.setAttribute("pad", pad);
		session.setAttribute("items", items);
//		map.addAttribute("pad", pad);
//		map.addAttribute("items", items);
		return "forward:cart.jsp";
	}

}
